<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        td {
            border: 1px solid red;
        }

        table {
            border-collapse: collapse;
            width: 400px;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="toAdd">添加</button>
        <table>
            <tr v-for="(item,index) in tableData" :key="item.id">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.address}}</td>
                <td><button @click="toDelete(item.id)">删除</button></td>
                <td><button @click="toEdit(item)">修改</button></td>
            </tr>
        </table>
        <!-- 模态框 -->
        <el-dialog :title="title" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="id" :label-width="formLabelWidth">
                    <el-input v-model="form.id" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="名字" :label-width="formLabelWidth">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" :label-width="formLabelWidth">
                    <el-input v-model="form.address" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
            </div>
        </el-dialog>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                // 标题
                title: "",
                //表单
                form: {},
                // 控制表单控件的宽度
                formLabelWidth: '120px',
                // 控制模态框显示与隐藏
                dialogFormVisible: false,
                // 表格数据
                tableData: [{
                    id: 1,
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }, {
                    id: 2,
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄'
                }, {
                    id: 3,
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄'
                }, {
                    id: 4,
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1516 弄'
                }]
            },
            methods: {
                // 添加按钮绑定事件
                toAdd() {
                    this.title = '添加数据';
                    this.dialogFormVisible = true;
                    this.form = {}
                },
                // 删除绑定的事件
                toDelete(id, index) {
                    this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        // 确认删除
                        // 发一个删除请求
                        // 通过索引删除当前行数据
                        this.tableData.splice(index, 1)
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                    // console.log(id);
                },
                // 修改绑定事件
                toEdit(item) {
                    this.title = '修改数据';
                    // 模态框显示
                    this.dialogFormVisible = true;
                    // 将每一行数据赋值给表单对象
                    this.form = { ...item };
                    console.log(item);
                }
            },
        })
    </script>
</body>

</html>